import java.util.Date;

/*
 *  This class defines the basic "product" object.
 *  A product has the following features:
 *  	- Name		(default name and brand)
 *  	- Quantity	(specified amount)
 *  	- Unit 		(the product's unit of measurement)
 *  	- Shop 		(name of shop where item was bought)
 *  	- Date 		(date item was bought/added to cart)
 */

public class Product implements Comparable<Product>
{
	private String	name;
	private Units	unit;
	private Integer	quantity;
	private String	shop;
	private Date	date;

	// Full Constructor used for products in History and in Loading;
	Product(String name, Integer quantity, String unit, String shop, Date date)
	{
		this.setName(name);
		this.quantity = quantity;
		this.setUnit(unit);
		this.setShop(shop);
		this.date = date;
	}

	// Constructor used for products in Cart.
	Product(String name, Integer quantity, String unit)
	{
		this(name, quantity, unit, "any", new Date());
	}

	// Constructor used for products in Database.
	Product(String name, String unit)
	{
		this(name, -1, unit);
	}

	private void setName(String name)
	{
		this.name = Read.clean(name);
	}

	void setQuantity(Integer quantity)
	{
		this.quantity = quantity;
	}

	private void setUnit(String unit)
	{
		try
		{
			this.unit = Units.valueOf(unit.trim().toLowerCase());
		}
		catch (Exception e)
		{
			this.unit = Units.getDefault();
		}
	}

	void setShop(String shop)
	{
		this.shop = Read.clean(shop);
	}

	String getName()
	{
		return this.name;
	}

	Integer getQuantity()
	{
		return this.quantity;
	}

	String getUnit()
	{
		return this.unit.toString();
	}

	String getShop()
	{
		return this.shop;
	}

	Date getDate()
	{
		return this.date;
	}

	void refreshDate()
	{
		this.date = new Date();
	}

	@Override
	public String toString()
	{
		if (this.quantity != -1)
		{
			return name + " (" + quantity.toString() + " " + unit.toString() + ")";
		}
		else
		{
			return name + " (" + unit.toString() + ")";
		}
	}

	
	@Override
	public int compareTo(Product anotherProduct)
	{
		String s1 = this.name;
		String s2 = anotherProduct.name;
	    return s1.compareTo(s2);
	}
}
